﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_ManageAdvertise : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindDataGrid(1);
        }
        else
        {

        }
    }
    #region Bind data to grid
    protected void BindDataGrid(int pageIndex)
    {
        try{
            grdAdvertise.DataSource = Advertise_session.GetList_Advertise();
            grdAdvertise.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    #endregion

    #region Delete Advertise
    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        
    }
    //Lay ID tren cac cot duoc checked	
    private string AdvertiseSelectedArray()
    {
        string strArrayID = "";
        foreach (GridViewRow rows in grdAdvertise.Rows)
        {
            CheckBox chk = (CheckBox)rows.FindControl("chkCheck");

            if (chk.Checked)
            {
                ImageButton btnDel = (ImageButton)rows.FindControl("btnDelete");
                if (strArrayID != "")
                {
                    strArrayID += ",";
                }
                strArrayID += btnDel.CommandArgument.Trim();
            }
        }
        return strArrayID;
    }

    #endregion

    #region Datagrid events
    protected void grdAdvertise_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdAdvertise.PageIndex = e.NewPageIndex;
        BindDataGrid(grdAdvertise.PageIndex);

    }
    protected void grdAdvertise_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strAdvertiseID = e.CommandArgument.ToString().Trim();
        string strCommand = e.CommandName;
        switch (strCommand)
        {

            case "delete_Advertise":
                try
                {
                    Advertise advertise = Advertise_session.Get_Advertise_ByID(int.Parse(strAdvertiseID));
                    if (advertise == null) return;
                    Advertise_session.Delete_Advertise(advertise);

                   // string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Adv/";
                  //  ExtendFunctional.DeleteImageInFolder(_sFolder + "/" + advertise.Adv_Image);
                    WebMsgBox.Show("Đã xóa thành công.");
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;
            case "active_Advertise":
                try
                {
                    Advertise advertise = Advertise_session.Get_Advertise_ByID(int.Parse(strAdvertiseID));
                    if (advertise == null) return;
                    advertise.Adv_Status = !advertise.Adv_Status;
                    Advertise_session.Update_Advertise(advertise);
                    WebMsgBox.Show("Đã kích hoạt thành công.");
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;
        }

    }

    protected void grdAdvertise_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgActive = (ImageButton)e.Row.FindControl("btnActive");
            if (imgActive != null)
            {
                object obj = DataBinder.Eval(e.Row.DataItem, "Adv_Status");
                if (obj != null)
                {
                    if (obj.ToString().ToLower() == "true")
                    {
                        imgActive.ImageUrl = "Images/check.gif";
                    }
                    else
                    {
                        imgActive.ImageUrl = "Images/uncheck.gif";
                    }
                }
            }
        }
    }
    #endregion

    #region Extend events/codes

    #endregion
}
